'use strict';

angular.module('xuexibangApp')
	.controller('HistoryCtrl', function($scope, Exam, Klass, Auth) {
		var me=Auth.getCurrentUser();
		var pagesize=10;
		$scope.pageindex=1;
		console.log(me);
		$scope.examlist=[];
		var klasscodes=[];
		$scope.klasses=[];
		$scope.selKlass = function(klass){
			$scope.klass=klass;
			Exam.getCount({code:klass.code},function(count){
				console.log(count.count);
				$scope.pagecount = count.count/pagesize;
			});
			getExams(klass,1);
		}
		var inicode=0;
		if(Auth.isRole('teacher')){
			if(me.klasses){
				klasscodes=me.klasses;
				inicode=klasscodes[0];
				if(me.chargeklass){
					inicode=me.chargeklass;
				}
			}else{
				return;//是教师，但没有教授任何班级
			}
		}else{
			if(me.klass){
				klasscodes.push(me.klass);
				inicode=klasscodes[0];
			}else{
				return;//可能是admin，没有班级
			}
		}
		Klass.get({code:inicode},function(klass){
			$scope.selKlass(klass);
		});
		$scope.isMine = function(name){
			return (me.name===name)&&Auth.isRole('teacher');
		}
		//console.log(klasscodes);
		klasscodes.forEach(function(code,key){
			Klass.get({code:code},function(klass){
				$scope.klasses.push(klass);
			})
		});
		$scope.selPage = function(index){
			$scope.pageindex = index;
			getExams($scope.klass,index);
		}
		function getExams(klass,pageindex){
			Exam.getOfKlass({code:klass.code,pageindex:pageindex,pagesize:pagesize},function(exams){
				//console.log(exams);
				$scope.exams=exams;
				//console.log($scope.klasses);
			},function(err){
				console.log(err);
			})
		}

		$scope.confirmDel = function(exam,exams){
			$scope.delexam=exam;//给删除确认提示框传递参数用
			$scope.delExams=exams;//给删除确认传递参数用
		}
		$scope.delExam = function(exam){
			Exam.delete({id:$scope.delexam._id},function(data){
				console.log("已经成功删除：",data);
				console.log($scope.delExams);
				_.remove($scope.delExams, {_id: $scope.delexam._id});
			},function(err){
				console.log(err);
			})
		}
		$scope.getNumber = function(num) {
			//if(!num) return [];
			console.log(num,Math.ceil(num));
    	return new Array(Math.ceil(num));
		}
	})
